package com.zzw.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.springboot.common.Result;
import com.zzw.springboot.entity.Course;
import com.zzw.springboot.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zzw2000
 * @Date 2022-06-10 16:17:35
 * @Description Course前端控制器
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private ICourseService courseService;

    /**
     * 保存
     *
     * @param course
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Course course) {
        return Result.success(courseService.saveOrUpdate(course));
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(courseService.removeById(id));
    }

    /**
     * 根据id批量删除course
     *
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public Result delete(@RequestBody List<Integer> ids) {
        return Result.success(courseService.removeBatchByIds(ids));
    }

    /**
     * 更改课程状态
     * @param course
     * @return
     */
    @PostMapping("/update")
    public Result changeEnable(@RequestBody Course course){
        return Result.success(courseService.updateById(course));
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        return Result.success(courseService.list());
    }

    /**
     * 查询单条记录
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(courseService.getById(id));
    }

    /**
     * 分页查询
     *
     * @param pageNum  当前页码
     * @param pageSize 每页显示的条数
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam(defaultValue = "") String name) {
        return Result.success(courseService.findPage(new Page<>(pageNum, pageSize),name));
    }

    /**
     * 学生选课
     * @param courseId 课程ID
     * @param studentId 学生ID
     * @return
     */
    @PostMapping("/studentCourse/{courseId}/{studentId}")
    public Result selectCourse(@PathVariable Integer courseId,@PathVariable Integer studentId){
        courseService.studentCourse(courseId,studentId);
        return Result.success();
    }


}

